define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/thread/index/index' + location.search,
                    add_url: 'facrm/thread/index/add',
                    // edit_url: 'facrm/thread/index/edit',
                    zhuanhuan_url: 'facrm/thread/index/zhuanhuan',
                    del_url: 'facrm/thread/index/del',
                    multi_url: 'facrm/thread/index/multi',
                    import_url: 'facrm/thread/index/import',
                    table: 'thread',
                }
            });

            var table = $("#table");



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageList: [10,100,999],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'remark', title: __('产品信息'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'number', title: __('数量'),operate:false},
                        {field: 'money', title: __('金额'),operate:false},
                        {field: 'from', title: __('来源'),operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'detail_address', title: __('Detail_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'thread_type_id', title: __('类别'),   visible: false,  searchList: $.getJSON("facrm/thread/type/indexs")},
                        {field: 'type.name', title: __('类别'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //获取选中项
            $(document).on("click", ".btn-edit_zhuanhuan", function () {
                var  info=Table.api.selectedids(table);
                Backend.api.open('./facrm/thread/index/zhuanhuan/ids/' + info, __('转换类别'));
            });


            //获取选中项
            $(document).on("click", ".btn-edit_fenpei", function () {
                var  info=Table.api.selectedids(table);
                Backend.api.open('./facrm/thread/index/fenpei/ids/' + info, __('分配信息'));
            });


            //导入
            $(document).on("click", ".btn-edit_sc", function () {
                var  info=Table.api.selectedids(table);
                Backend.api.open('./facrm/thread/index/daoru/ids/' , __('导入信息'));
            });


            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        zhuanhuan: function () {
            Controller.api.bindevent();
        },

        fenpei: function () {
            Controller.api.bindevent();
        },

        daoru:function (){

            require(['upload'], function(Upload){
                Upload.api.plupload();
            });

            Controller.api.bindevent();
        },
        
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
